package kr.co.kiso.usr.main;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.kiso.usr.am.gr.service.AMGRService;
import kr.co.kiso.usr.am.gr.vo.AMGRVo;
import kr.co.kiso.usr.common.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/dsmgr/*")
public class MainController extends BaseController{
	@Autowired
	private AMGRService amgrService;
	
	/**
	 * 관리자 메인화면
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView index(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView();
		return mav;
	}
	
	/**
	 * 로그인 체크 하는 프로세스
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr180Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = null;


//		vo.setAdmin_pw(secu.Enc("ADMIN_USR","admin_pw",vo.getAdmin_pw()));
		
		
		Map map =amgrService.amgr180Q(vo);
		
		if(map==null){
			mav = new ModelAndView("/common/close");
			mav.addObject("msg","아이디나 비밀번호가 틀립니다");
			mav.addObject("redirect","./index.do");
		}else{
			//메뉴랑 권한 가져오기는 쿼리
			
			List topRoles = amgrService.amgr200Q(vo);
			Map data = (Map) topRoles.get(0);
			int data1 =Integer.parseInt( String.valueOf( data.get("MENU_SEQ_NO")));
			vo.setMenu_seq_no(data1);
			
			
			List list = amgrService.amgr190Q(vo);
			
//			Cookie cookie = new Cookie("menuTopSeq", String.valueOf( data.get("MENU_SEQ_NO")));
//			cookie.setMaxAge(0);
//			res.addCookie(cookie);

			
			req.getSession().setAttribute("menuList", list);
			req.getSession().setAttribute("menuTopSeq", data1);
			req.getSession().setAttribute("topRoles", topRoles);
			req.getSession().setAttribute("userInfo", map);
			mav = new ModelAndView("redirect:/dsmgr/main.do");
		}
		
		return mav;
	}
	
	/**
	 * 관리자 메인화면
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView main(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		isMember(req,res);
		
		ModelAndView mav = new ModelAndView();

		
		return mav;
	}
	
	/**
	 * 세션 초기화 로그아이웃
	 * @param vo
	 * @param req
	 * @param res
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public ModelAndView amgr200Q(AMGRVo vo, HttpServletRequest req,
			HttpServletResponse res) throws Exception {

		ModelAndView mav = new ModelAndView("redirect:/dsmgr/index.do");
		
		req.getSession().invalidate();
		return mav;
	}
}
